package com.sxbbc.api.controller;

import java.util.List;
import java.util.Map;

import com.sxbbc.common.core.dto.jz.*;
import com.sxbbc.common.core.entity.jz.*;
import com.sxbbc.common.core.enums.*;
import com.sxbbc.common.core.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.api.common.BusinessMapping;
import com.sxbbc.api.service.IBusinessService;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.service.system.login.LoginTokenService;
import com.sxbbc.common.core.utils.BasePage;

/**
 * 店铺接口 控制层
 *
 * @author xiaoky
 * @date 2019-05-27
 */
@RestController

public class BusinessController {

    @Autowired
    private IProcDescribeService iProcDescribeService;
    @Autowired
    private ICShoppingCartService icShoppingCartService;
    @Autowired
    private IProSkuDetailService iProSkuDetailService;
    @Autowired
    private IIndustryService industryService;
    @Autowired
    private IBOfflineService ibOfflineService;
    @Autowired
    private IBusinessService iBusinessService;
    @Autowired
    private LoginTokenService loginTokenService;
    @Autowired
    private ProcClassifyShopService classifyShopService;
    @Autowired
    private BBusinessService bBusinessService;
    @Autowired
    private ICCollectBusService collectBusService;
    @Autowired
    private IProcMainService procMainService;
    @Autowired
    private ISysCashRuleService sysCashRuleService;
    @Autowired
    private IInfoParamService iInfoParamService;

    /**
     * 查询附近商家所有有效行业
     *
     * @return 行业列表 不分页
     */
    @RequestMapping(value = BusinessMapping.QUERY_BUSINESS_INDUSTRY)
    public RestResponse queryBusinessIndustry() {
        QueryWrapper<Industry> wrapper = new QueryWrapper<>();
        wrapper.eq("status", StaticUtils.STATUS_YES);
        wrapper.orderByDesc("sort");
        List<Industry> industryList = industryService.list(wrapper);
        return GetRest.getSuccess("", industryList);
    }

    /**
     * 查询附近商家列表
     *
     * @param business 附近商家查询条件
     * @param page     分页
     * @return RestResponse 附近商家数据
     */
    @RequestMapping(value = BusinessMapping.QUERY_NEAR_BUSINESS)
    public RestResponse queryNearBusiness(BasePage<BBusiness> page, BBusiness business) {
        IPage<NearBusinessVo> pageInfo = iBusinessService.queryNearBusiness(page, business);
        return GetRest.getSuccess("", pageInfo);
    }

    /**
     * 查询附近商家详情
     *
     * @param bid 商家id
     * @return 附近商家对应数据
     */
    @RequestMapping(value = BusinessMapping.GET_NEAR_BUSINESS_DETAIL)
    public RestResponse getBusinessDetail(Integer bid) {
        NearBusinessDetailVo detail = iBusinessService.getBusinessDetail(bid);
        return GetRest.getSuccess("", detail);
    }

    //region 附近商家购买商品（自营类型，走线上物流，非自营类型，走自提）

    /**
     * 查询附近商家分类列表
     *
     * @param bid 店铺id
     * @return 数据集
     */
    @RequestMapping(value = BusinessMapping.QUERY_BUSINESS_CLASSIFY)
    public RestResponse queryBusinessClassify(Integer bid) {
        QueryWrapper<ProcFirstClassifyShop> wrapper = new QueryWrapper<>();
        wrapper.eq("target_id", bid).eq("type", ClassifyTypeEnums.BUSINESS.getId()).eq("status", StaticUtils.STATUS_YES);
        List<ProcFirstClassifyShop> classifyList = classifyShopService.queryClassifyList(wrapper);
        return GetRest.getSuccess("success", classifyList);
    }

    /**
     * 根据分类查询商家商品
     *
     * @param page    分页
     * @param bid     商家ID
     * @param firstId 分类ID
     * @return com.function.common.RestResponse
     * @author leehao
     */
    @RequestMapping(value = BusinessMapping.QUERY_BUSINESS_PRODUCT)
    public RestResponse queryBusinessProduct(BasePage<ProcMain> page, Integer bid, Integer firstId) {
        BBusiness bBusiness = bBusinessService.getById(bid);
        if (bBusiness == null) {
            return GetRest.getFail("该店铺不存在");
        }
        IPage<NearBusinessProlVo> iPage = procMainService.queryNearBusinessProductList(page, bid, firstId);
        return GetRest.getSuccess("success", iPage);
    }

    /**
     * 查询附近商家商品详情
     *
     * @param token
     * @param productVo
     * @return
     */
    @RequestMapping(value = BusinessMapping.QUERY_BUSINESS_PRODUCT_DETAIL)
    public RestResponse showProductDetail(String token, ProductVo productVo) {
        if (StringUtils.isNotBlank(token)) {
            Integer cid = loginTokenService.validLoginCid(token);
            if (cid != null) {
                productVo.setCid(cid);
            }
        }
        System.out.println("附近商家商品详情访问token" + token);
        //商品信息
        ProductDetailVo productDetailVo = procMainService.selectProductSimpleDetail(productVo);
        return GetRest.getSuccess("", productDetailVo);
    }

    //endregion

    //region 附近商家买单

    /**
     * 获取附近商家买单页面 金额与创业分比例
     *
     * @param token 获取cid
     */
    @RequestMapping(value = BusinessMapping.MONEY_TO_POINT)
    public RestResponse getBuyMsg(String token, Integer bid) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        if (bid == null) {
            return GetRest.getFail("参数缺少，请求错误");
        }
        BBusiness business = bBusinessService.getById(bid);
        return GetRest.getSuccess("", business.getScale());
    }

    /**
     * 获取附近商家买单页面数据
     *
     * @param token 获取cid
     * @param bid
     * @param scan  1 扫码进入  其他数据都是非扫码
     * @return Map封装，余额，店铺名
     */
    @RequestMapping(value = BusinessMapping.GET_BUY_MSG)
    public RestResponse getBuyMsg(String token, Integer bid, Integer scan) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        Map<String, Object> map = iBusinessService.getBuyMsg(bid, cid, scan);
        return GetRest.getSuccess("", map);
    }

    /**
     * 附近商家买单支付之后显示页面数据
     *
     * @param token   获取cid
     * @param payCode 支付单号
     * @return 付款信息
     */
    @RequestMapping(value = BusinessMapping.GET_NEAR_PAY_AFTER)
    public RestResponse getNearBuyAfter(String token, String payCode) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        Map<String, Object> map = iBusinessService.getNearBuyAfter(cid, payCode);
        return GetRest.getSuccess("", map);
    }

    /**
     * 处理附近商家线下买单
     *
     * @param token      获取cid
     * @param offline    买单数据
     * @param payWord    支付密码
     * @param payChannel 支付渠道 PayChannelEnums
     * @return 支付宝或微信支付返回签名信息
     */
    @RequestMapping(value = BusinessMapping.HANDLE_OFFLINE_BUY)
    public RestResponse handleOfflineBuy(String token, BOffline offline, String payWord, Integer payChannel) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        offline.setCid(cid);
        RestResponse result = iBusinessService.handleOfflineBuy(offline, payWord, payChannel);
        return result;
    }

    /**
     * 获取商家线下买单记录
     *
     * @param token   获取cid
     * @param payCode 交易單號
     */
    @RequestMapping(value = BusinessMapping.GET_OFFLINE_BUY)
    public RestResponse getOfflineBuy(String token, String payCode) {
        Integer cid = loginTokenService.validLoginCid(token);
        if (StringUtils.isBlank(token) || cid == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        QueryWrapper<BOffline> wrapper = new QueryWrapper<>();
        wrapper.eq("cid", cid).eq("pay_code", payCode);
        BOffline bOffline = ibOfflineService.getOne(wrapper);
        if (bOffline.getBid() != null) {
            BBusiness business = bBusinessService.getById(bOffline.getBid());
            bOffline.setPayee(StringUtils.isBlank(business.getNickName()) ? business.getNickNameOn() : business.getNickName());
        } else {
            bOffline.setPayee("平台自营");
        }
        return GetRest.getSuccess("", bOffline);
    }

    //endregion
}
